﻿using System;
using System.Threading;
using System.Windows.Forms;

namespace Microsoft.Office.OneNote.XmlViewer
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(state => 
            {
                hierarchyTree.LoadHierarchy();
                BeginInvoke(new Action(() => 
                {
                    Cursor = Cursors.Default;
                    statusLabel.Text = "Done";
                }));
            });

            base.OnLoad(e);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (AboutForm form = new AboutForm())
            {
                form.ShowDialog(this);
            }
        }

        private void OnSelectedNodeChanged(object sender, TreeViewEventArgs e)
        {
            delay.Stop();
            delay.Start();
        }

        private void OnSelectedNodeChanging(object sender, TreeViewCancelEventArgs e)
        {
            contentViewer1.LeaveCurrentObject();
        }

        private void delay_Tick(object sender, EventArgs e)
        {
            delay.Enabled = false;

            if (hierarchyTree.IsHierarchyXmlNodeSelected)
            {
                contentViewer1.Navigate(OneNoteHierarchy.Current);
            }
            else
            {
                contentViewer1.Navigate(hierarchyTree.SelectedHierarchyObject);
            }
        }

        private void contentViewer1_StatusTextChanged(object sender, EventArgs e)
        {
            //statusLabel.Text = contentViewer1.StatusText;
        }
    }
}
